<template>
  <div id="headerSimpleWithShare" class="headerSimpleWithShare">
    <x-header :left-options="{showBack: false}">
	    <x-icon slot="overwrite-left" type="ios-arrow-back" size="16" class="backIcon" @click="goBack"></x-icon>
	    <span slot="overwrite-left" style="color: white; font-weight: bold; font-size: 16px">{{title}}</span>
	    <img v-if="isApp" slot="right" width="16" style="display:block;margin-right:10px;margin-top: 2px;" src="@/assets/svg/icon_wechat_share.svg" @click="showMenus = true">
    </x-header>

	  <div v-transfer-dom>
		  <actionsheet :menus="menus" v-model="showMenus" @on-click-menu="click" show-cancel></actionsheet>
	  </div>

  </div>
</template>

<script>
	import bus from '@/event/eventBus'
  import { XHeader,TransferDom,Actionsheet } from 'vux'

  export default {
    name: 'topSimpleWithShare',
	  directives: {
		  TransferDom
	  },
    components: {
      XHeader,
	    TransferDom,
	    Actionsheet
    },
    data () {
      return {
      	title: '引领右脑分享',
	      menus: {
		      shareWithFriends: '分享给朋友',
		      shareWithTimeLine: '分享到朋友圈'
	      },
	      showMenus: false,
	      shareWithFriendsParams: {},
	      isApp: true
      }
    },
	  created: function () {
		  if(!navigator.userAgent.match('DuoZhaoApp')){
			  this.isApp = false
		  }

    	let _this = this
		  bus.$on("setTopSimpleWithShareTitle", function (msg) {
			  _this.title = msg
		  })

		  bus.$on("setShareWithFriends", function (msg) {
			  _this.shareWithFriendsParams = msg
		  })
	  },
	  methods: {
		  goBack() {
			  this.$router.go(-1)
		  },
		  click (key) {
			  if(key == 'shareWithFriends'){
				  this.wechatShareWithFriends()
			  }else if(key == 'shareWithTimeLine'){
				  this.wechatSharWithTimeLine()
			  }
		  },
		  wechatShareWithFriends() {

			  if (typeof Wechat === "undefined") {
				  alert("Wechat plugin is not installed.");
				  return false;
			  }

			  Wechat.share({
				  message: {
				  	title: this.shareWithFriendsParams.title,
					  description: this.shareWithFriendsParams.description,
					  thumb: this.shareWithFriendsParams.thumb,
					  media: {
				  		type: this.shareWithFriendsParams.media_type,
						  webpageUrl: this.shareWithFriendsParams.media_webpageUrl
					  }
				  },
				  scene: Wechat.Scene.SESSION
			  }, function () {
			  	//alert("Success")
			  }, function (reason) {
			  	alert("Failed: " + reason)
			  });
		  },
		  wechatSharWithTimeLine() {
			  if (typeof Wechat === "undefined") {
				  alert("Wechat plugin is not installed.");
				  return false;
			  }

			  Wechat.share({
				  message: {
					  title: this.shareWithFriendsParams.title,
					  description: this.shareWithFriendsParams.description,
					  thumb: this.shareWithFriendsParams.thumb,
					  media: {
						  type: this.shareWithFriendsParams.media_type,
						  webpageUrl: this.shareWithFriendsParams.media_webpageUrl
					  }
				  },
				  scene: Wechat.Scene.TIMELINE
			  }, function () {
				  //alert("Success");
			  }, function (reason) {
				  alert("Failed: " + reason);
			  });
		  }
	  }
  }
</script>

<style>

	.headerSimpleWithShare > .vux-header > .vux-header-title {
		text-align: left;
		padding-left: 45px;
	}

	.vux-header {
		background-color: #44abe2 !important;
	}

	.headerSimpleWithShare {
		width: 100%;
		position: fixed;
		left: 0px;
		top: 0px;
		z-index: 9999;
	}

	.backIcon {
		fill: #fff;
		position: relative;
		margin-right: 15px;
		top: 2px !important;
		left: 5px !important;
	}
</style>
